<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Response
 *
 * @author Jewel Tolentino
 */
class Response {

    //put your code here


    public static final function create($parentDoc, $workflowData) {
        
        $auth = Auth::getAuth('current_user');
        $date = $parentDoc->currentDateTime();
        $form_id = $workflowData['workflow-form-trigger'];
        $actions = $workflowData['workflow-field_formula'];

        $requestFormDoc = new Form($parentDoc, $parentDoc->formId);
        $formDoc = new Form($parentDoc, $form_id);
        $personDoc = new Person($parentDoc, $auth['id']);

        $workflow_object = functions::getFormActiveWorkflow($formDoc);
        $workflow_json = json_decode($workflow_object["json"], true);
        $result = array();

        $result['Requestor'] = $auth['id'];
        $result['Processor'] = $auth['id'];
//        $result['Status'] = 'Draft';
//        $result['imported'] = '1';
        $result['DateCreated'] = $date;
        $result['DateUpdated'] = $date;
        $result['Workflow_ID'] = $workflow_object["workflow_id"];
        $result['Node_ID'] = $workflow_json["workflow-default-action"];

        foreach ($actions as $key) {
            $field_name = $key['workflow-field-update'];
            $formula = $key['workflow-field-formula'];

            $formulaDoc = new Formula($formula);

            $formulaDoc->setSourceForm($requestFormDoc->form_name, "*", array(
                array("FieldName" => "ID",
                    "Operator" => "=",
                    "Value" => $parentDoc->id))
            );
            $returnValue = $formulaDoc->evaluate();

            $result[$field_name] = $returnValue;
        }

        print_r($result);
        $requestDoc = new Request();
        $requestDoc->load($form_id, '0');
        $requestDoc->data = $result;
        $requestDoc->save();
        
        $requestDoc->processWF();
        $requestLog = new Request_Log($parentDoc);
        $requestLog->form = $formDoc;
        $requestLog->request_id = $requestDoc->id;
        $requestLog->details = 'Created from ' . $parentDoc->formName . ' with Tracking Number : ' . $parentDoc->trackNo;
        $requestLog->created_by = $personDoc;
        $requestLog->date_created = $date;
        $requestLog->save();
    }

    public static final function update() {
        
    }

}
